package db

import (
    `errors`
    `fmt`
    `github.com/jmoiron/sqlx`
    "github.com/op/go-logging"
)

var log = logging.MustGetLogger("db")

type Query string

type Queryx struct {
    Query  Query
    Params []interface{}
}

type DB struct {
    *sqlx.DB
}

type Tx struct {
    *sqlx.Tx
}
type selectOption interface {
    Wrap(string, []interface{}) (string, []interface{})
}
type limitOption struct {
    offset int
    count  int
}

var (
    ErrNoGetterFound   = errors.New("No getter found")
    ErrNoDeleterFound  = errors.New("No deleter found")
    ErrNoSelecterFound = errors.New("No getter found")
    ErrNoUpdaterFound  = errors.New("No updater found")
    ErrNoInserterFound = errors.New("No inserter found")
)

func Limit(offset, count int) selectOption {
    return &limitOption{offset, count}
}

func (o *limitOption) Wrap(query string, params []interface{}) (string, []interface{}) {
    query = fmt.Sprintf("SELECT a.* from (%s) a LIMIT ?,?", query)
    params = append(params, o.offset)
    params = append(params, o.count)
    return query, params
}

func (tx *Tx) Selectx(o interface{}, qx Queryx, options ...selectOption) error {
    q := string(qx.Query)
    params := qx.Params

    log.Debug(q)
    for _, option := range options {
        q, params = option.Wrap(q, params)
    }
    if u,ok := o.(Selecter);ok{

    }

}


// https://hackernoon.com/how-to-work-with-databases-in-golang-33b002aa8c47
//  http://www.golangprograms.com/example-of-golang-crud-using-mysql-from-scratch.html
// https://www.cnblogs.com/suoning/p/7259106.html
// https://jaehue.github.io/post/go-my-way-3-tracing/
// https://yoshikawataiki.net/posts/summer-internship-2018/